<template>
  <div class="qualiy_list">

    <div class="monitor-tree">
      <h3 class="tree-title">选择隧道</h3>
      <el-tree
        :data="tunnelInfoChild"
        @check-change="handleCheckChange"
        :default-expanded-keys="keysListNodeData"
        :highlight-current="true"
        :accordion="true"
        :check-on-click-node="true"
        node-key="pid"
        ref="tree"
      ></el-tree>
    </div>

    <div class="monitor-list">
    <MonitorReportHeader @onReshMonitorReport="changeData" />
    <el-table :data="MonitorReport" :cell-style="{'text-align':'center'}" :header-cell-style="{background: '#f8f8f9',textAlign: 'center'}">
      <el-table-column width="200" show-overflow-tooltip prop="reportNo" label="报告编号"></el-table-column>
      <el-table-column width="100" show-overflow-tooltip prop="reportType" label="报告类型">
        <template slot-scope="scope">
            <el-tag v-if="scope.row.reportType == 1" >周报</el-tag>
            <el-tag v-if="scope.row.reportType == 2" type="success">月报</el-tag>
            <el-tag v-if="scope.row.reportType == 3" type="warning">预警公文</el-tag>
          </template>
      </el-table-column>
      <el-table-column  show-overflow-tooltip prop="createTime" label="报告时间"></el-table-column>
      <el-table-column  label="操作">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="primary"
              icon="el-icon-search"
              @click="preview(scope.$index, scope.row)"
            >预览</el-button>
          <el-button size="mini" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.$index, scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @current-change="handleCurrentChange"
        layout="total, sizes, prev, pager, next, jumper"
        :total="currentPage.total"
      ></el-pagination>
    <MonitorReportEditor :flag="flag" />
    <MonitorReportWeek :flag="flag" />
    <MonitorReportMonth :flag="flag" />
    </div>
   
  </div>
</template>

<script>
import base from '../../../api/base'
import api from '../../../api'
import qs from 'qs'
import MonitorReportEditor from './MonitorReportEditor.vue'
import MonitorReportHeader from './MonitorReportHeader.vue'
import MonitorReportWeek from './MonitorReportWeek.vue'
import MonitorReportMonth from './MonitorReportMonth.vue'

export default {
  data() {
    return {
      keysListNodeData:[],
      pdfsrc:'',
      tId:'',
      tunnelInfoChild: [],
      MonitorReport: [],
      flag: false,
      currentPage: {
        pageNo: 1,
        total: 0
      }
    }
  },
  components: {
    MonitorReportEditor,
    MonitorReportHeader,
    MonitorReportMonth,
    MonitorReportWeek
  },

  mounted() {
    this.getchildren()
    this.$bus.$on('onRefreshMonitorReport', (flag) => {
      this.http(1,flag)
      this.currentPage.pageNo = 1
    })
  },
  methods: {
       //获取数结构
       getchildren() {
      api
        .tunnelInfoChildFindByAll()
        .then((res) => {
          if (res.data.code == 2001) {
            this.tunnelInfoChild = res.data.data
            const arrDat = res.data.data[0].pid
            this.keysListNodeData.push(arrDat)
          }
        })
    },
    http(page,tId) {
      api
        .monitorReportqueryPage({ page,tId })
        .then((res) => {
          if (res.data.code == 2001) {
            this.MonitorReport = res.data.data.records
            this.currentPage.total = parseInt(res.data.data.total)
          }
        })
    },
    handleEdit(index, row) {
      this.$bus.$emit('onShowTunnelDirectionEditor', {rowdata:row,tId:this.tId})
    },
    handleDelete(index, row) {
      let data = {
        id: row.id
      }
      this.$confirm('此操作将永久删除隧道方向, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        customClass: 'del-model',
        type: 'warning'
      })
        .then(() => {
          api
            .monitorReportdeleteById(qs.stringify(data))
            .then((res) => {
              if (res.data.code == 2001) {
                this.$message({
                  type: 'success',
                  message: '删除成功!'
                })
                // 视图重绘
                this.http(1,this.tId)
              } else {
                this.$message({
                  type: 'fail',
                  message: '删除失败!'
                })
              }
            })
            .catch((err) => {
              console.log(err)
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    handleCurrentChange(val) {
      this.http(val,this.tId)
    },
    //树结构选中
    handleCheckChange(data, checked, node) {
      if (data.tid != undefined) {
        this.tId = data.tid
        this.http(1, this.tId)
        this.$bus.$emit('onAddMonitorReportTid', this.tId)
        this.$bus.$emit('MonitorReportWeekFid', this.tId)
        this.$bus.$emit('MonitorReportMonthFid', this.tId)
      }
    },
    preview(index, row) {
      this.pdfsrc = base.filebaseUrl + row.resources
      this.$router.push('/pdf/' + encodeURIComponent(this.pdfsrc))
    },
    changeData(type){
      api
        .monitorReportfindByType({ type })
        .then((res) => {
          if (res.data.code == 2001) {
            this.MonitorReport = res.data.data.records
            this.currentPage.total = parseInt(res.data.data.total)
          }
        })
    }
  }
}
</script>

<style scoped>

.tree-title {
  text-align: left;
  font-size: 16px;
  color: #606266;
  padding: 10px;
  padding-bottom: 20px;
}
.monitor-tree {
  float: left;
  width: 300px;
  border-left: 1px solid #f1f1f1;
  border-right: 1px solid #f1f1f1;
  background-color: #fff;
}
.monitor-list {
  float: right;
  margin-left: 10px;
  width: 100%;
}
.block {
  position: absolute;
  bottom: 50px;
  right: 20px;
}
</style>